package GUILayer;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import CtrLayer.SalesOrderCtr;
import ModelLayer.SalesOrder;

public class OrderListTableModel  extends AbstractTableModel {
	private SalesOrderCtr soCtr;
	private ArrayList<SalesOrder> li = new ArrayList<SalesOrder>();
	private String[] columnNames = { "Order ID", "Date", "Customer ID",
			"Status", "Delivery Date", "Price",
			"Unique Products"};
	
	public OrderListTableModel(){
		soCtr = SalesOrderCtr.getInstance();
	}
	public void populateList()
	{
		ArrayList<SalesOrder> tmpList = soCtr.listSalesOrders();
		System.out.println(tmpList.size());
		if(tmpList != null)
		{
			this.li.clear();
			this.li = tmpList;
		}
		fireTableDataChanged();
	}

	public void populateList(String filter) {
		ArrayList<SalesOrder> tmpList = soCtr.listSalesOrders(filter);
		System.out.println(tmpList.size());
		if(tmpList != null)
		{
			this.li.clear();
			this.li = tmpList;
		}
		fireTableDataChanged();
	}
	
	@Override
	public String getColumnName(int columnIndex){
		return columnNames[columnIndex];
	}

	@Override     
	public int getRowCount() {
		return li.size();
	}

	@Override        
	public int getColumnCount() {
		return 7;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		SalesOrder so = li.get(rowIndex);
		switch (columnIndex) {
		case 0: 
			return so.getOrderId();
		case 1:
			return so.getDate();
		case 2:
			return so.getC().getPhoneNo();
		case 3:
			return so.getDeliveryStatus();
		case 4:
			return so.getDeliveryDate();
		case 5:
			return so.getTotalPrice();
		case 6:
			return so.getPo().size();	
		}
		return null;
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex){
		switch (columnIndex){
		case 0:
			return Integer.class;
		case 1:
			return String.class;
		case 2:
			return String.class;
		case 3:
			return String.class;
		case 4:
			return String.class;
		case 5:
			return Double.class;
		case 6:
			return Integer.class;
		}
		return null;
	}
}